package serviceimpl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import lib.ExecuteLocal;
import lib.MySQLLoaclHelper;

public class BBB {

	private static final SimpleDateFormat df =  new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
	public static void main(String[] args) throws MalformedURLException, IOException, ParseException, SQLException, InterruptedException {
		
		String[] types = new String[]{"hot","goufangzhengce","xuanfangxuzhi","fangchanjiaoyi"};
		String sql = "insert into Article (countent,createTime,description,gonglueLocation,indexLocation,label,listImage,status,title,type,updateTime,source) value(?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object[]> paramsList = new ArrayList<>();
		Document sss = Jsoup.parse(new URL("http://www.baihejia.com/gonglue"), 3000);
		Element hwslider = sss.getElementById("hwslider");
		Elements lis = hwslider.select("li");
		Map<String, String> map = new HashMap<>();
		for (Element li : lis) {
			String url = li.select("a").get(0).attr("href");
			if(url.contains("recommend")){
				url = "http://www.baihejia.com"+url;
				String img = li.select("img").get(0).attr("src");
				System.out.println(url);
				Document detailDoc = Jsoup.parse(new URL(url), 3000);
				Elements select = detailDoc.select("[class=content]");
				Element element = select.get(0);
				String title = element.select("[class=title]").get(0).text();
				long time = df.parse(element.select("li").get(0).text()).getTime();
				String main = element.select("[class=mainContent]").get(0).html().replaceAll("<img", "<p> <img style=\"max-width:100%\"").replaceAll("alt=\"相关图片\">", "></p>");
				String desp = element.select("em").text().replace("【摘要】", "");
				paramsList.add(new Object[] {main,time,desp,1,0,"",img,1,title,0,time,"百合家"});
			}
		}
		Elements moreImages = sss.getElementsByClass("moreImages").get(0).select("li");
		for (Element moreImage : moreImages) {
			String url = moreImage.select("a").get(0).attr("href");
			int i = 2;
			if(url.contains("recommend")){
				url = "http://www.baihejia.com"+url;
				String img = moreImage.select("img").get(0).attr("data-original");
				System.out.println(url);
				Document detailDoc = Jsoup.parse(new URL(url), 3000);
				Elements select = detailDoc.select("[class=content]");
				Element element = select.get(0);
				String title = element.select("[class=title]").get(0).text();
				long time = df.parse(element.select("li").get(0).text()).getTime();
				String main = element.select("[class=mainContent]").get(0).html().replaceAll("<img", "<p> <img style=\"max-width:100%\"").replaceAll("alt=\"相关图片\">", "></p>");
				String desp = element.select("em").text().replace("【摘要】", "");
				paramsList.add(new Object[] {main,time,desp,i,0,"",img,1,title,0,time,"百合家"});
				i++;
			}
		}
		for (String type : types) {
			Document hot = Jsoup.parse(new URL("http://www.baihejia.com/gonglue?type="+type), 3000);
			Elements eles = hot.getElementsByClass("newslist01");
			for (Element ele : eles) {
				String src = ele.select("a").get(0).attr("href");
				if(map.containsKey(src)){
					map.remove(src);
				}
				String img = ele.select("img").get(0).attr("data-original");
				String desp = ele.select("p").get(0).text();
				String url = "http://www.baihejia.com"+src;
				System.out.println(url);
				Document detailDoc = Jsoup.parse(new URL(url), 3000);
				Elements select = detailDoc.select("[class=content]");
				Element element = select.get(0);
				String title = element.select("[class=title]").get(0).text();
				long time = df.parse(element.select("li").get(0).text()).getTime();
				String main = element.select("[class=mainContent]").get(0).html().replaceAll("<img", "<p> <img style=\"max-width:100%\"").replaceAll("alt=\"相关图片\">", "></p>");
				int typeint = 0;
				if(type.equals("hot")){
					typeint = 1;
				}else if(type.equals("goufangzhengce")){
					typeint = 2;
				}else if(type.equals("xuanfangxuzhi")){
					typeint = 3;
				}else if(type.equals("fangchanjiaoyi")){
					typeint = 4;
				}
				paramsList.add(new Object[] {main,time,desp,0,0,"",img,1,title,typeint,time,"百合家"});
			
			}
			Thread.sleep(3000);
		}
		MySQLLoaclHelper.withTransaction(new ExecuteLocal() {
			@Override
			public void call() throws Exception {
				executeBatch(sql, paramsList);
			}
		});
	}
}
